﻿using Engine.Graphics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Forms;
using System.Xml;

namespace UnitTestsGameDll
{
    [TestClass]
    public class Material
    {
        #region Private Fields

        private Engine.Engine engine;
        private Form form;
        private MaterialManager materialManager;

        #endregion Private Fields

        #region Public Methods

        [ClassInitialize()]
        public void Init()
        {
            this.form = new Form();
            this.form.Size = new System.Drawing.Size(500, 500);
            System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
            panel.Size = new System.Drawing.Size(500, 500);
            this.form.Controls.Add(panel);

            this.engine = new Engine.Engine(panel, this.form.Handle);

            this.materialManager = new Engine.Graphics.MaterialManager(this.engine, ".\\texture\\");
        }

        [TestMethod]
        public void XmlMaterialReadWrite()
        {
            Engine.Graphics.IMaterial materialLoad;

            //Init();

            WorldRacing2.Material.Wr2Material material = new WorldRacing2.Material.Wr2Material();
            material.TextureID1 = 0;
            material.TextureID2 = 0;
            material.TextureID3 = 0;
            material.UpdateTextureName(this.materialManager);

            string xmlFilename = ".\\material.xml";

            using (XmlTextWriter sw = new XmlTextWriter(xmlFilename, System.Text.Encoding.ASCII))
            {
                sw.Formatting = Formatting.Indented;
                sw.Indentation = 4;
                material.ToXml(sw);
            }

            using (XmlReader reader = XmlReader.Create(xmlFilename))
            {
                materialLoad = WorldRacing2.Material.Wr2Material.FromXmlStream(reader, this.materialManager);
            }

            Assert.AreEqual(material.TextureName1, ((WorldRacing2.Material.Wr2Material)materialLoad).TextureName1);
            Assert.AreEqual(material.TextureName2, ((WorldRacing2.Material.Wr2Material)materialLoad).TextureName2);
            Assert.AreEqual(material.TextureName3, ((WorldRacing2.Material.Wr2Material)materialLoad).TextureName3);
            Assert.AreEqual(material.TextureID1, ((WorldRacing2.Material.Wr2Material)materialLoad).TextureID1);
            Assert.AreEqual(material.TextureID2, ((WorldRacing2.Material.Wr2Material)materialLoad).TextureID2);
            Assert.AreEqual(material.TextureID3, ((WorldRacing2.Material.Wr2Material)materialLoad).TextureID3);
            Assert.AreEqual(material.Mappings1.CRC, ((WorldRacing2.Material.Wr2Material)materialLoad).Mappings1.CRC);
            Assert.AreEqual(material.Mappings2.CRC, ((WorldRacing2.Material.Wr2Material)materialLoad).Mappings2.CRC);
            Assert.AreEqual(material.Mappings3.CRC, ((WorldRacing2.Material.Wr2Material)materialLoad).Mappings3.CRC);
        }

        #endregion Public Methods
    }
}